package com.itheima.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.ShoppingCartDao;
import com.itheima.ShoppingCartService;
import com.itheima.bean.ShoppingCart;
import com.itheima.config.ThreadLocalConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Transactional
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartDao shoppingCartDao;
    @Override
    public int jiang(String dishId, String setmealId) {
        LambdaQueryWrapper<ShoppingCart> scqw = new LambdaQueryWrapper<>();
        scqw.eq(dishId!=null,ShoppingCart::getDishId,dishId);
        scqw.eq(setmealId!=null,ShoppingCart::getSetmealId,setmealId);
        scqw.eq(ShoppingCart::getUserId, ThreadLocalConfig.get());
        ShoppingCart shoppingCart = shoppingCartDao.selectOne(scqw);
        Integer number = shoppingCart.getNumber();

        if (number==1){
            return    shoppingCartDao.delete(scqw);
        }
        Integer n=number-1;
        shoppingCart.setNumber(n);

        BigDecimal amount = shoppingCart.getAmount();

        BigDecimal amount1 = amount.divide(BigDecimal.valueOf((Integer) number), 2, BigDecimal.ROUND_HALF_UP);
        BigDecimal amount2 = amount1.multiply(BigDecimal.valueOf((Integer) n));
        shoppingCart.setAmount(amount2);

        return  shoppingCartDao.update(shoppingCart,scqw);
    }

    @Override
    public void deleteAll(Long userId) {
        LambdaQueryWrapper<ShoppingCart> qw=new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId,userId);
        shoppingCartDao.delete(qw);
    }

    @Override
    public List<ShoppingCart> show(Long userId) {
        LambdaQueryWrapper<ShoppingCart> qw=new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId,userId);
        return shoppingCartDao.selectList(qw);
    }

    @Override
    public int add(ShoppingCart shoppingCart) {
        shoppingCart.setCreateTime(LocalDateTime.now());
        LambdaQueryWrapper<ShoppingCart> qw=new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId,shoppingCart.getUserId());
        int number=1;
        if (shoppingCart.getDishId()!=null){
            qw.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else {
            qw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
       List <ShoppingCart> list = shoppingCartDao.selectList(qw);
        if (list!=null){
            for (ShoppingCart shoppingCart1 : list) {
                 if(shoppingCart1.getDishFlavor()==null){
                     int number1 = shoppingCart1.getNumber();
                     number+=number1;
//           shoppingCart1.setAmount(shoppingCart1.getAmount()+shoppingCart.getAmount());
                     shoppingCart1.setAmount(shoppingCart1.getAmount().add(shoppingCart.getAmount()));
                     shoppingCart1.setNumber(number);
                     return shoppingCartDao.updateById(shoppingCart1);
                 }


                if (shoppingCart1.getDishFlavor().equalsIgnoreCase(shoppingCart.getDishFlavor())){
                    int number1 = shoppingCart1.getNumber();
                    number+=number1;
//           shoppingCart1.setAmount(shoppingCart1.getAmount()+shoppingCart.getAmount());
                    shoppingCart1.setAmount(shoppingCart1.getAmount().add(shoppingCart.getAmount()));
                    shoppingCart1.setNumber(number);
                    return shoppingCartDao.updateById(shoppingCart1);
                }else {

                    return shoppingCartDao.insert(shoppingCart);
                }
//                number+= shoppingCart1.getNumber();
//
////           shoppingCart1.setAmount(shoppingCart1.getAmount()+shoppingCart.getAmount());
//                shoppingCart1.setAmount(shoppingCart1.getAmount().add(shoppingCart.getAmount()));
//                shoppingCart1.setNumber(number);
//                return shoppingCartDao.updateById(shoppingCart1);
            }


        }

            return shoppingCartDao.insert(shoppingCart);
    }
}
